import config from './config.js'

const ceshi = function(){
	console.log(config.webUrl) 
	ceshi2()
	ceshi3.three('three')
}
const ceshi2 = function(){
	console.log("测试2方法")
}

const ceshi3 = {
	first:false,
	two:false,
	three:function(data){
		console.log('测试3:'+data)
		console.log(this.first)
	}
}

// 网络监听
const NetWork = {
	isConnect:false,
	On : function(){
		// 获取当前的网络状态
		uni.getNetworkType({
			success:(res)=>{
				// {"errMsg":"getNetworkType:ok","networkType":"4g"}
				console.log(JSON.stringify(res))
				if(res.networkType!='none'){this.isConnect = true; 
					uni.showToast({
						title:"你现在处于"+res.networkType+'网络',
						icon:"none"
					})
				 return;}
				uni.showToast({
					title:"离线状态",
					icon:"none"
				})
				
			}
		})
		
		uni.onNetworkStatusChange((res) => {
		    console.log(res.isConnected);
			if(res.isConnected)
			this.isConnect = res.isConnected
			if(!res.isConnected){
				uni.showToast({
					title:"你现在处于离线状态",
					icon:"none"
				})
				return;
			}
		   switch(res.networkType){
			   case 'wifi':
				uni.showToast({
					title:"你现在处于"+res.networkType+ '状态',
					icon:'none'
				})
				break;
				case '2g':
					uni.showToast({
						title:"你现在处于"+res.networkType+ '状态',
						icon:'none'
					})
					break;
				case '3g':
					uni.showToast({
						title:"你现在处于"+res.networkType+ '状态',
						icon:'none'
					})
					break;
				case '4g':
					uni.showToast({
						title:"你现在处于"+res.networkType+ '状态',
						icon:'none'
					})
					break;
				case '5g':
					uni.showToast({
						title:"你现在处于"+res.networkType+ '状态',
						icon:'none'
					})
					break;	
		   }
		});
		
	}
}

// 热更新
const upData = ()=>{
	// #ifdef APP-PLUS  
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
	    uni.request({  
	        url: 'http://www.example.com/update/',  
	        data: {  
	            version: widgetInfo.version,  
	            name: widgetInfo.name  
	        },  
	        success: (result) => {  
	            var data = result.data;  
	            if (data.update && data.wgtUrl) {  
	                uni.downloadFile({  
	                    url: data.wgtUrl,  
	                    success: (downloadResult) => {  
	                        if (downloadResult.statusCode === 200) {  
	                            plus.runtime.install(downloadResult.tempFilePath, {  
	                                force: false  
	                            }, function() {  
	                                console.log('install success...');  
	                                plus.runtime.restart();  
	                            }, function(e) {  
	                                console.error('install fail...');  
	                            });  
	                        }  
	                    }  
	                });  
	            }  
	        }  
	    });  
	});  
	// #endif
}
export default {
	ceshi,
	NetWork,
	upData
}